/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 jfinal app. jfapp Group.
 */

package app.models.wx;

import app.models.member.Member;
import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.ColumnDef;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * <p>
 * The table olb_wx_ticket mapping model.
 * </p>
 */
@TableBind(tableName = "olb_wx_ticket")
public class WxTicket extends Model<WxTicket> {

    private static final long   serialVersionUID = 1L;
    /**
     * The public dao.
     */
    public static final WxTicket dao = new WxTicket();


    public List<WxTicket> list(){
    	return find(SqlKit.sql("wxticket.list"));
    }

    public Page<WxTicket> customerTickets(DatatablesCriterias criterias, Member member, WxTicket ticket, String uploadTime) {
        int pageSize = criterias.getDisplaySize();
        int start = criterias.getDisplayStart() / pageSize + 1;
        final List<ColumnDef> sortingColumnDefs = criterias.getSortingColumnDefs();
        StringBuilder sql = new StringBuilder(" FROM olb_wx_ticket ticket, ol_member member WHERE ticket.member=member.id");
        List<Object> params = Lists.newArrayList();
        if (member != null) {
            String cardNo = member.getStr("card_no");
            if (!Strings.isNullOrEmpty(cardNo)) {
                sql.append(" AND member.card_no like ? ");
                params.add(StringPool.PERCENT + cardNo + StringPool.PERCENT);
            }

            String phone = member.getStr("phone");
            if (!Strings.isNullOrEmpty(phone)) {
                sql.append(" AND member.phone like ? ");
                params.add(StringPool.PERCENT + phone + StringPool.PERCENT);
            }
        }
        if (ticket != null) {
            Boolean added = ticket.getBoolean("added");
            if (added != null) {
                sql.append(" AND ticket.added = ? ");
                params.add(added);
            }

            if (!Strings.isNullOrEmpty(uploadTime)) {
                sql.append(" AND ticket.upload_time between ? AND ? ");
                params.add(uploadTime + " 00:00:00");
                params.add(uploadTime + " 23:59:59");
            }
        }
        if (sortingColumnDefs != null && !sortingColumnDefs.isEmpty()) {
            StringBuilder orderBy = new StringBuilder();
            for (ColumnDef sortingColumnDef : sortingColumnDefs)
                if (sortingColumnDef.isSortable()) {
                    orderBy.append(sortingColumnDef.getName()).append(StringPool.SPACE).append(sortingColumnDef.getSortDirection().toString());
                }
            final String byColumns = orderBy.toString();
            if (!Strings.isNullOrEmpty(byColumns)) {
                sql.append(" ORDER BY ").append(byColumns);
            }
        }
        if (!StringUtils.contains(sql, "ORDER")) {
            sql.append(" ORDER BY ticket.added, ticket.upload_time DESC ");
        }
        return paginate(start, pageSize, SqlKit.sql("wxticket.customer_columns"), sql.toString(), params.toArray());
    }


    public WxTicket findByTicketNo(String ticketNo) {
        return findFirst(SqlKit.sql("wxticket.findByTicketNo"), ticketNo);
    }
}